<?php

class FHProfileForm extends Zend_Form
{
    private $picture_path = "";
    private $logo_path = "";
	private $funeral_home = null;
	private $num_open_cases = 0;
	private $vasid = '';
	private $quick_stats = '';
	private $branch_number = 0;
	private $branch_tstats = '';
	private $branch_rstats = '';
	private $region_tstats = '';
	private $region_rstats = '';
	private $keystone_tstats = '';
	private $keystone_rstats = '';
	
    public function __construct($options=array())
    {
        parent::__construct($options);

        if(isset($options['fh_picture_path']))
            $this->picture_path = $options['fh_picture_path'];

        if(isset($options['fh_logo_path']))
            $this->logo_path = $options['fh_logo_path'];
		$this->branch_number = $options['branch_number'];
		$this->funeral_home = $options['funeral_home'];
		$VAS_fh_id = $options['VASfh_id'];
		$this->vasid = $options['VASfh_id'];
		$this->quick_stats = $options['quick_stats'];
		if($options['num_open_cases']==1)
			$this->num_open_cases = sprintf("Current Open cases: %d", $options['num_open_cases']);
		else
			$this->num_open_cases = sprintf("Current Open cases: %d", $options['num_open_cases']);
		$this->branch_tstats = $options['branch_timing'];
		$this->branch_rstats = $options['branch_revenue'];
		$this->region_tstats = $options['region_timing'];
		$this->region_rstats = $options['region_revenue'];
		$this->keystone_tstats = $options['keystone_timing'];
		$this->keystone_rstats = $options['keystone_revenue'];
//		$this->num_open_cases = "{$options['num_open_cases']}";
		
        // set form name and POST
        $this->setName('fh_profile');
        $this->setMethod('post');
        $this->setEnctype('multipart/form-data');

        $decorators = array(
            'ViewHelper',
//            array(array('labelBr'=>'HtmlTag'), array('tag'=>'br')),
            array('Errors', array('placement'=>'append')),
            array('Label', array('placement'=>'prepend')),
            array( array('outerDiv'=>'HtmlTag'), array('tag'=>'div', 'style'=>'margin-top:5px')),
            
        );
		
        $cbdecorators = array(
            'ViewHelper',
            //array('Errors', array('placement'=>'prepend')),
            //array('Label', array('placement'=>'append'))
            array('Errors', array('placement'=>'append')),
            array('Label', array('placement'=>'prepend', 'style'=>'width:120px;'))
        );
		
        $picdecorators = array(
            'ViewHelper',
            array(array('labelBr'=>'HtmlTag'), array('tag'=>'br', 'clear'=>'all')),
            array('Label', array('placement'=>'prepend', 'style'=>'width:120px; text-align:left;')),
            array( array('outerDiv'=>'HtmlTag'), array('tag'=>'div', 'style'=>'margin-top:5px')),
        );
		
		$style = 'font-weight:bold;';
	
		$this->vasid = $options['VASfh_id'];
		
		$f_id = new Zend_Form_Element_Hidden('funeralhome_id');
		$f_id->setValue($options["VASfh_id"]);

		$f_mgrname = new Zend_Form_Element_Text('manager_name', array('label'=>'Manager:', 'size'=>'30', 'style'=>$style));
		$f_mgrname->setDecorators($decorators);

		$f_region = new Zend_Form_Element_Text('region', array('label'=>'Region:', 'size'=>'15', 'style'=>$style));
		$f_region->setDecorators($decorators);
		
		$f_name = new Zend_Form_Element_Text('name', array('label'=>'Name:', 'readonly'=>true, 'size'=>'30', 'style'=>$style));
		$f_name->setDecorators($decorators);
		$f_address = new Zend_Form_Element_Text('addr1', array('label'=>'Address:', 'readonly'=>true, 'size'=>'30', 'style'=>$style));
		$f_address->setDecorators($decorators);
		$f_city = new Zend_Form_Element_Text('city', array('label'=>'City:', 'readonly'=>true, 'size'=>'30', 'style'=>$style));
		$f_city->setDecorators($decorators);
		$f_state = new Zend_Form_Element_Text('state', array('label'=>'State:', 'readonly'=>true, 'size'=>'2', 'style'=>$style));
		$f_state->setDecorators($decorators);
		$f_zip = new Zend_Form_Element_Text('zip', array('label'=>'Zip:', 'readonly'=>true, 'size'=>'10', 'style'=>$style));
		$f_zip->setDecorators($decorators);
		$f_phone = new Zend_Form_Element_Text('phone', array('label'=>'Phone:', 'required'=>'true', 'size'=>'16', 'style'=>$style));
		$f_phone->addValidator('NotEmpty',true,array('messages'=>array('isEmpty'=>'Required')));
		$f_phone->setDecorators($decorators);
        // funeral home picture uploader
		$f_email = new Zend_Form_Element_Text('primary_email', array('label'=>'Email:', 'required'=>'true', 'size'=>'30', 'style'=>$style));
		$f_email->addValidator('NotEmpty',true,array('messages'=>array('isEmpty'=>'Required')));
		$f_email->setDecorators($decorators);
		
		$f_url = new Zend_Form_Element_Text('url', array('label'=>'Website:', 'required'=>'true', 'size'=>'30', 'style'=>$style));
		$f_url->addValidator('NotEmpty',true,array('messages'=>array('isEmpty'=>'Required')));
		$f_url->setDecorators($decorators);
		
		$f_show_tributes = new Zend_Form_Element_Checkbox( 'show_packages', array('label'=>'Show packages?' ));
		$f_show_tributes->setDecorators($cbdecorators);
			
        $funeral_home_picture = new Zend_Form_Element_File('photo_image');
        $funeral_home_picture->setLabel('Upload a photo:')
			->setRequired(false)
	        ->setDestination(ROOT_DIR.'/upload')
	        ->addValidator('Count', 1)     // ensure only 1 file
	        ->addValidator('Size', false, 502400) // limit to 500K
	        ->addValidator('Extension', false, 'jpg,png,gif'); // only JPEG, PNG, and GIFs
	    $funeral_home_picture->setDecorators($picdecorators);

        $funeral_home_logo = new Zend_Form_Element_File('logo_image');
        $funeral_home_logo->setLabel('Upload a logo:')
			->setRequired(false)
	        ->setDestination(ROOT_DIR.'/upload')
	        ->addValidator('Count', 1)     // ensure only 1 file
	        ->addValidator('Size', false, 502400) // limit to 100K
	        ->addValidator('Extension', false, 'jpg,png,gif'); // only JPEG, PNG, and GIFs
	    $funeral_home_logo->setDecorators($picdecorators);

        // form's submit button
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Submit');

        $this->addElements(
            array(
				$f_id, $f_name, $f_address, $f_city, $f_state, $f_zip, $f_phone, $f_url,
				$f_mgrname,$f_region,$f_email,$f_show_tributes,$funeral_home_picture, $funeral_home_logo
            )
        );
		
		$this->addElements(array($submit));
		
    }
    
    // dirt-simple template engine.
    public function render_with_template( $template_name )
    {
        $template_file_path = APP_DIR."/views/templates/$template_name";
        $template_contents = file_get_contents( $template_file_path );

        // str_replace() keywords, adding errors where needed
        $form_elements = $this->getElements();

		$template_contents = str_replace( "{FORM_ACTION}", $this->getAction(), $template_contents );
		$template_contents = str_replace( "{FORM_ENCODING}", $this->getEnctype(), $template_contents );
	
		$branch = "<input type=hidden name=\"branch_number\" value=\"{$this->branch_number}\">";	
        $template_contents = str_replace( "{BRANCH_NUMBER}", $branch, $template_contents );
		$hidden = "<input type=hidden name=\"funeralhome_id\" value=\"{$this->vasid}\">";	
        $template_contents = str_replace( "{FUNERAL_HOME_ID}", $hidden, $template_contents );

        $template_contents = str_replace( "{FUNERAL_HOME_ID}", $form_elements['funeralhome_id'], $template_contents );
        $template_contents = str_replace( "{FUNERAL_HOME_REGION}", $form_elements['region'], $template_contents );
        $template_contents = str_replace( "{FUNERAL_HOME_MANAGER}", $form_elements['manager_name'], $template_contents );
        $template_contents = str_replace( "{FUNERAL_HOME_NAME}", $form_elements['name'], $template_contents );
        $template_contents = str_replace( "{FUNERAL_HOME_ADDR1}", $form_elements['addr1'], $template_contents );
        $template_contents = str_replace( "{FUNERAL_HOME_CITY}", $form_elements['city'], $template_contents );
        $template_contents = str_replace( "{FUNERAL_HOME_STATE}", $form_elements['state'], $template_contents );
        $template_contents = str_replace( "{FUNERAL_HOME_ZIP}", $form_elements['zip'], $template_contents );
        $template_contents = str_replace( "{FUNERAL_HOME_PHONE}", $form_elements['phone'], $template_contents );
        $template_contents = str_replace( "{FUNERAL_HOME_FAX}", $form_elements['fax'], $template_contents );
        $template_contents = str_replace( "{FUNERAL_HOME_PRIMARY_EMAIL}", $form_elements['primary_email'], $template_contents );
 
        $template_contents = str_replace( "{FUNERAL_HOME_SHOW_PKGS}", $form_elements['show_packages'], $template_contents );
		
 		$template_contents = str_replace( "{FUNERAL_HOME_URL}", $form_elements['url'], $template_contents );
 		
 		$template_contents = str_replace( "{FUNERAL_HOME_PIC_PATH}", $this->picture_path, $template_contents );
        $template_contents = str_replace( "{FUNERAL_HOME_LOGO_PATH}", $this->logo_path, $template_contents ); 
		
		$template_contents = str_replace( "{FUNERAL_HOME_PIC_FILE}", $form_elements['photo_image'], $template_contents );
        $template_contents = str_replace( "{FUNERAL_HOME_LOGO_FILE}", $form_elements['logo_image'], $template_contents );
		
		$template_contents = str_replace( "{BRANCH_TIMING}", $this->branch_tstats, $template_contents );
		$template_contents = str_replace( "{BRANCH_REVENUE}", $this->branch_rstats, $template_contents );
		$template_contents = str_replace( "{REGION_TIMING}", $this->region_tstats, $template_contents );
		$template_contents = str_replace( "{REGION_REVENUE}", $this->region_rstats, $template_contents );
		$template_contents = str_replace( "KEYSTONE_TIMING", $this->keystone_tstats, $template_contents );
		$template_contents = str_replace( "{KEYSTONE_REVENUE}", $this->keystone_rstats, $template_contents );
		
        $template_contents = str_replace( "{NUMOPENCASES}", $this->num_open_cases, $template_contents );

        $template_contents = str_replace( "{SUBMIT_BUTTON}", $this->submit, $template_contents );

		$template_contents = str_replace( "{QUICK_STATS}", $this->quick_stats, $template_contents );
		
        echo $template_contents;
    }
}

?>
